import React from 'react';
import PropTypes from 'prop-types';
import {Card, Button, Form, Row, Col} from 'antd';
import {FORM_OPTIONS} from '../constants';
import {partClaimStatusHis} from '../Enum';
import TextInput from 'Shared/components/TextInput';
import WrappedSelect from '../common/WrappedSelect';
import DealerSelectPanel from '../common/DealerSelect/SelectEdit';
import routes from './routes';
import {injectIntl, language} from './intl';
const FormItem = Form.Item;

const partClaimStatusList = partClaimStatusHis.toList(language);
class HisQueryPanel extends React.PureComponent {
    handleFilterChange = (value, name) => {
        this.props.onConditionsChange(name, value);
    }
    handleFilterPressEnter = (value, name) => {
        this.props.onConditionsChange(name, value);
        this.props.querySearch();
    }
    onSelectDealer = data => {
        this.props.onConditionsChange('dealerId', data.id);
        this.props.onQueryDealerChange({
            id: data.id,
            code: data.code,
            name: data.name,
        });
    }
    onCancel = () => {
        this.props.history.push(routes.query.url());
    }
    render() {
        const dealerInfo = this.props.conditions.dealerId ? {
            id: this.props.queryDealer.id,
            code: this.props.queryDealer.code,
            name: this.props.queryDealer.name
        } : null;
        return (
            <div>
                <Card>
                    <Form className="form-standard">
                        <Row>
                            {!this.props.isDealerUser &&
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem
                                        label={this.props.intl.formatMessage({
                                            id: 'hisQueryPanel.form.dealerId',
                                            defaultMessage: '总代'
                                        })}
                                        {...FORM_OPTIONS.item}>
                                        <DealerSelectPanel
                                            value={dealerInfo}
                                            onSelect={this.onSelectDealer} />
                                    </FormItem>
                                </Col>
                            }
                            <Col {...FORM_OPTIONS.col}>
                                <FormItem
                                    label={this.props.intl.formatMessage({
                                        id: 'hisQueryPanel.form.code',
                                        defaultMessage: '编号'
                                    })}
                                    {...FORM_OPTIONS.item}>
                                    <TextInput
                                        name="code"
                                        value={this.props.conditions.code}
                                        onPressEnter={this.handleFilterPressEnter}
                                        onBlur={this.handleFilterChange} />
                                </FormItem>
                            </Col>
                            <Col {...FORM_OPTIONS.col}>
                                <FormItem
                                    label={this.props.intl.formatMessage({
                                        id: 'hisQueryPanel.form.status',
                                        defaultMessage: '状态'
                                    })}
                                    {...FORM_OPTIONS.item}>
                                    <WrappedSelect
                                        allowClear
                                        name="status"
                                        value={this.props.conditions.status}
                                        options={partClaimStatusList}
                                        onChange={this.handleFilterChange} />
                                </FormItem>
                            </Col>
                        </Row>
                        <Row>
                            <Col span={6}>
                                <Button key="query" type="primary" onClick={this.props.querySearch} loading={this.props.loading}>
                                    {this.props.intl.formatMessage({
                                        id: 'hisQueryPanel.btn.query',
                                        defaultMessage: '查询'
                                    })}
                                </Button>
                                <Button key="clear" onClick={this.props.onResetClick}>
                                    {this.props.intl.formatMessage({
                                        id: 'hisQueryPanel.btn.reset',
                                        defaultMessage: '重置'
                                    })}
                                </Button>
                            </Col>
                        </Row>
                    </Form>
                </Card>
                <div className="page-toolbar">
                    <Button type="primary" onClick={this.onCancel}>{this.props.intl.formatMessage({
                        id: 'hisQueryPanel.operation.onCancel',
                        defaultMessage: '返回'
                    })}</Button>
                </div>
            </div>
        );
    }
}

HisQueryPanel.propTypes = {
    conditions: PropTypes.object.isRequired,
    loading: PropTypes.bool.isRequired,
    querySearch: PropTypes.func.isRequired,
    onConditionsChange: PropTypes.func.isRequired,
    onQueryDealerChange: PropTypes.func.isRequired,
    onResetClick: PropTypes.func.isRequired,
    history: PropTypes.object,
    intl: PropTypes.object,
    isDealerUser: PropTypes.bool,
    queryDealer: PropTypes.object,
};

import {connect} from 'react-redux';
import {
    saveQueryConditionHis,
    resetQueryPanelHis,
    querySearchHis,
    saveQueryDealerHis
} from './actions';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';

const getConditions = selectorFactory(['page', 'appState', 'queryConditionHis']);
const getQueryDealer = selectorFactory(['page', 'appState', 'queryDealerHis']);
const mapStateToProps = state => ({
    loading: state.getIn((['page', 'domainData', 'listHis', 'isFetching'])),
    isDealerUser: state.getIn((['page', 'domainData', 'initData', 'isDealerUser'])),
    conditions: getConditions(state),
    queryDealer: getQueryDealer(state),
});

const mapDispatchToProps = dispatch => ({
    onConditionsChange: (name, value) => dispatch(saveQueryConditionHis(name, value)),
    onQueryDealerChange: data => dispatch(saveQueryDealerHis(data)),
    querySearch: () => dispatch(querySearchHis()),
    onResetClick: () => dispatch(resetQueryPanelHis()),
});
export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(HisQueryPanel));
